package com.bai.edu.controller;


import com.bai.base.exceptionHandler.BaiException;
import org.springframework.util.StringUtils;
import com.bai.edu.client.VodClient;
import com.bai.edu.entity.Video;
import com.bai.edu.service.VideoService;
import com.bai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/eduservice/video")
public class VideoController {
   @Autowired
    private VideoService videoService;
   @Autowired
   private VodClient vodClient;

   //添加
   @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video){
       videoService.save(video);
       return R.ok();
   }

   //删除
    @DeleteMapping("remove/{id}")
    public R removeVideo(@PathVariable String id){
       //根据小节id获取视频id
        Video video = videoService.getById(id);
        String sourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(sourceId)){
            R r = vodClient.removeVideo(sourceId);
            if (r.getCode()==201){
                throw new BaiException(201,"删除视频失败，熔断器...");
            }
        }
       videoService.removeById(id);
       return R.ok();
    }

    //修改小节
    @PostMapping("uodateVideo")
    public R updateVide(@RequestBody Video video){
       videoService.updateById(video);
       return R.ok();
    }
}

